from random import randint, choice
from time import sleep

import pypinyin
from selenium.webdriver.common.by import By

import page
from base.base import Base
from tool.get_log import GetLogger
from tool.write_json import write_dict_to_json

log = GetLogger.get_log()


class PageConversationBot(Base):
    def page_init(self):
        log.info('正在进行page初始化操作')
        size = self.driver.get_window_size()
        self.base_find_element((By.XPATH, "//*[@text='发现']"))
        self.driver.swipe(size['width'] * 0.5, size['height'] * 0.4, size['width'] * 0.5, size['height'] * 0.9)
        self.base_click_element(page.init_xcx_name)
        sleep(5)
        self.driver.switch_to.context(self.driver.contexts[-1])
        # self.base_click_element(page.fp_signin_close)
        try:
            log.info("正在跳过首页签到弹窗")
            self.base_click_element(page.fp_signin_close)
            log.info("首页签到弹窗已关闭")
        except:
            log.info("未找到首页弹窗")
            pass

    def page_conversation_back_url(self):
        # pass
        for i in range(3):
            sleep(5)
            try:
                self.base_find_element(page.chat_baping_btn)
                break
            except:
                self.driver.press_keycode(4)

    def page_conversation_entrance(self):
        self.base_scroll_element_into_view_js(page.fp_chat_group_tab, istop='false')
        self.base_click_element(page.fp_chat_group_tab)
        self.base_scroll_element_into_view_js(page.fp_chat_group, istop='false')
        while True:
            self.base_click_element(page.fp_chat_group)
            if self.base_if_element_exist(page.chat_baping_btn, timeout=5):
                break
        try:
            self.base_click_element(page.chat_desk_num_close,timeout=5)
        except:
            pass

    def page_conversation_click_baping(self):
        self.base_click_element(page.chat_baping_btn)

    def page_conversation_init_baping_paras(self):
        self.page_conversation_click_baping()
        el_list = self.base_find_elements(page.chat_baping_theme_list)
        time_list = ["15", "20", "30", "60", "120", "520"]
        case_num = 0
        case_dict = {}
        for i in el_list:
            sub_id = i.get_attribute("id")
            sub_loc = By.XPATH, f'//*[@id ="{sub_id}"]/wx-view/wx-view', page.chat_url
            sub_text = self.base_get_element_text(sub_loc)
            index = randint(0, 5)
            time_loc = By.CSS_SELECTOR, f"#\\{index + 30}", page.chat_url
            time = time_list[index]
            msg = "".join(pypinyin.lazy_pinyin(sub_text))
            case_dict.update(
                {
                    f"case{case_num}": {
                        "sub_loc": sub_loc,
                        "sub_name": sub_text,
                        "time_loc": time_loc,
                        "time": time,
                        "msg": msg
                    }
                }
            )
            case_num += 1
        write_dict_to_json(case_dict, 'baping_paras.json')
        self.base_click_element(page.chat_baping_menu_close)

    def page_conversation_send_baping(self, sub_loc, time_loc, msg):
        self.base_scroll_element_into_view_js(tuple(sub_loc), istop="false")
        sleep(1)
        self.base_click_element(tuple(sub_loc))
        sleep(1)
        self.base_click_element(tuple(time_loc))
        self.base_input_value_adb(page.chat_baping_input_msg, msg)
        try:
            self.base_click_element(page.chat_baping_coin_pay,timeout=5)
        except:
            pass
        self.base_click_element(page.chat_baping_send_btn)

    def page_conversation_get_baping_time(self):
        return self.base_get_element_text(page.chat_baping_assert_time)

    def page_conversation_get_baping_msg(self):
        return self.base_get_element_text(page.chat_baping_assert_msg)

    def page_conversation_click_dainifei(self):
        self.base_click_element(page.chat_dainifei_btn)

    def page_conversition_send_dainifei1(self):
        sleep(1)
        self.base_click_element(page.chat_dainifei_kind1)
        sleep(2)
        self.base_click_element(page.chat_dainifei_time30)
        self.base_input_value_adb(page.chat_dainifei_input_msg, "daoqitiaozhanzhe")
        self.base_click_element(page.chat_dainifei_coin_pay)
        self.base_click_element(page.chat_dainifei_send_btn)

    def page_conversition_send_dainifei2(self):
        sleep(1)
        self.base_click_element(page.chat_dainifei_kind2)
        sleep(2)
        self.base_click_element(page.chat_dainifei_time60)
        self.base_input_value_adb(page.chat_dainifei_input_msg, "lanbojini")
        self.base_click_element(page.chat_dainifei_coin_pay)
        self.base_click_element(page.chat_dainifei_send_btn)

    def page_conversation_get_dainifei_time(self):
        return self.base_get_element_text(page.chat_dainifei_assert_time)

    def page_conversation_get_dainifei_msg(self):
        return self.base_get_element_text(page.chat_dainifei_assert_msg)

    def page_conversation_click_baping_song(self):
        self.base_click_element(page.chat_baping_song_btn)

    def page_conversation_select_song(self):
        self.base_click_element(page.song_hot_rank)
        song_name_list = []
        song_select_num = 0
        for i in range(50):
            if song_select_num >= 3:
                break
            song_loc_tem = By.XPATH, f"//*[@class='boxVal']/*[@id='{i}']", page.song_url
            song_is_not_valid = By.XPATH, f"//*[@class='boxVal']/*[@id='{i}']//*[text()='已点']", page.song_url
            song_name_loc_tem = By.XPATH, f"//*[@class='boxVal']/*[@id='{i}']//*[@class='musicListItemLfName']", page.song_url
            if self.base_if_element_exist(song_is_not_valid, timeout=3):
                self.base_up_to_down_windowscroll_js(song_is_not_valid, offset=120)
                continue
            else:
                self.base_up_to_down_windowscroll_js(song_loc_tem, offset=120)
                self.base_click_element(song_loc_tem)
                song_name_list.append(self.base_get_element_text(song_name_loc_tem))
                song_select_num += 1
        self.base_click_element(page.song_bottom_btn)
        return song_name_list

    def page_conversation_send_song(self):
        self.base_input_value_adb(page.song_input_msg, "testsongmsg")
        self.base_click_element(page.song_coin_pay)
        self.base_click_element(page.song_send_btn)
        self.base_click_element(page.song_close_send_result)
        self.base_click_element(page.song_back_to_chat)

    def page_conversation_song_card_name(self):
        self.base_click_element(page.chat_song_open_more)
        return [
            self.base_get_element_text(page.chat_song_name1_assert),
            self.base_get_element_text(page.chat_song_name2_assert),
            self.base_get_element_text(page.chat_song_name3_assert)
        ]

    def page_conversation_song_card_msg(self):
        return self.base_get_element_text(page.chat_song_msg_assert)

    def page_conversation_song_card_num(self):
        return self.base_get_element_text(page.chat_song_num_assert)

    def page_conversation_click_song_selected_tab(self):
        self.base_click_element(page.song_selected_tab)

    def page_conversation_get_selected_names(self):
        self.base_swipe_to_bottom(times=10)
        els = self.base_find_elements(page.song_selected_titles)
        name_list = []
        for i in range(-1, -4, -1):
            name_list.append(els[i].text)
        return name_list

    def page_conversrion_song_back_to_chat(self):
        self.base_click_element(page.song_back_to_chat)

    def page_conversation_click_dashang(self):
        self.base_click_element(page.chat_dashang_btn)

    def page_conversation_init_dashang_paras(self):
        self.page_conversation_click_dashang()
        el_list = self.base_find_elements(page.chat_dashang_theme_list)
        obj_list = ["200", "201", "202", "203"]
        case_num = 0
        case_dict = {}
        for i in el_list:
            sub_id = i.get_attribute("data-id")
            sub_loc = By.XPATH, f'//*[@class="giveGiftPopBoxVal-gifts"]//*[@data-id ="{sub_id}"]/wx-view[1]', page.chat_url
            sub_text = self.base_get_element_text(sub_loc)
            obj_data_id = choice(obj_list)
            obj_loc = By.XPATH, f"//*[@data-id ='{obj_data_id}']/wx-view[2]", page.chat_url
            obj = self.base_get_element_text(obj_loc)
            msg = "".join(pypinyin.lazy_pinyin(sub_text))
            case_dict.update(
                {
                    f"case{case_num}": {
                        "sub_loc": sub_loc,
                        "sub_name": sub_text,
                        "obj_loc": obj_loc,
                        "obj": obj,
                        "msg": msg
                    }
                }
            )
            case_num += 1
        write_dict_to_json(case_dict, 'dashang_paras.json')
        self.base_click_element(page.chat_dashang_menu_close)

    def page_conversation_send_dashang(self, sub_loc, obj_loc, msg):
        self.base_scroll_element_into_view_js(tuple(sub_loc), istop="false")
        sleep(1)
        self.base_click_element(tuple(sub_loc))
        sleep(1)
        self.base_click_element(tuple(obj_loc))
        sleep(1)
        self.base_input_value_adb(page.chat_dashang_input_msg, msg)
        try:
            self.base_click_element(page.chat_dashang_coin_pay)
        except:
            pass
        self.base_click_element(page.chat_dashang_send_btn)

    def page_conversation_get_dashang_gift(self):
        return self.base_get_element_text(page.chat_dashang_assert_gift)

    def page_conversation_get_dashang_obj(self):
        return self.base_get_element_text(page.chat_dashang_assert_obj)

    def page_conversation_get_dashang_msg(self):
        return self.base_get_element_text(page.chat_dashang_assert_msg)